package leftMain;

//package org.dlsu.mt.parser;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.Vector;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;

public class ViewDictionaryGUI extends JFrame implements MouseListener{

	private JList list;
	private JComboBox comboLanguage;
	private JButton buttonExit;
	private JButton buttonLoad;
	private JLabel caption;
	private JSeparator line;
	
	
	public ViewDictionaryGUI(int x, int y){
		
		//Font italics = new java.awt.Font("Microsoft Sans Serif", 2, 16);
		//Font regular = new java.awt.Font("Microsoft Sans Serif", 1, 16)
		
		setTitle("View Dictionary");
		setResizable(false);
		setAlwaysOnTop(true);
		setBounds((x+100), (y+100), 500, 500);
		getContentPane().setLayout(null);
		addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				dispose();				
			}
		});		
		
		list = new JList();
		list.addMouseListener(this);
		
		list.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 16));
		
		JScrollPane scrollPane = new JScrollPane(list);		
		scrollPane.setBounds(30, 75, 430, 380);
		getContentPane().add(scrollPane);
		
		caption = new JLabel("Words --- Part of speech");
		caption.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 12));
        caption.setBounds(30, 40, 430, 25);
		getContentPane().add(caption);
		
		line = new JSeparator();
		line.setBounds(25, 65, 440, 20);
		getContentPane().add(line);
		
		comboLanguage = new JComboBox();
		comboLanguage.setFont(new java.awt.Font("Microsoft Sans Serif", 2, 17));
		comboLanguage.setBounds(20, 5, 240, 30);
		comboLanguage.addItem("Please choose dictionary. . .");
		comboLanguage.addItem("English Dictionary");
		comboLanguage.addItem("Filipino Dictionary");
		getContentPane().add(comboLanguage);
		
		buttonLoad = new JButton("Load");
		buttonLoad.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
        buttonLoad.setToolTipText("Load");
		buttonLoad.setBounds(270, 5, 100, 30);
		buttonLoad.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
            	buttonLoadActionPerformed();
            }
        });        
		getContentPane().add(buttonLoad);
		
		buttonExit = new JButton("Exit");
		buttonExit.setFont(new java.awt.Font("Microsoft Sans Serif", 1, 17));
        buttonExit.setToolTipText("Exit");
		buttonExit.setBounds(375, 5, 100, 30);
		buttonExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
				dispose();
            }
        });
        getContentPane().add(buttonExit);
        
		setVisible(true);		
	}
	
	
	public void mouseClicked(MouseEvent e)
	{
		if (e.getClickCount() == 2) {
                         
             if(list.getSelectedIndex() >= 0){
             
	             String temp = (String)list.getSelectedValue();
	             String[] arrTemp = temp.split(" --- ");	             
	             SearchWordGUI sw = new SearchWordGUI(this.getX(), this.getY(), arrTemp[0], arrTemp[1], comboLanguage.getSelectedIndex());
	         }
          }		
	}
	public void mouseEntered(MouseEvent e)
	{
		
	}
	public void mouseExited(MouseEvent e)
	{
		
	}
	public void mousePressed(MouseEvent e)
	{
		
	}
	public void mouseReleased(MouseEvent e)
	{
		
	}
	
	public void buttonLoadActionPerformed(){
		
		String query = "";
		
		if(comboLanguage.getSelectedIndex() == 0){
			javax.swing.JOptionPane.showMessageDialog(this, "Choose dictionary.", "View Dictionary", javax.swing.JOptionPane.OK_OPTION);
			return;
		}
		
		list.clearSelection();
		
		try{
   			
   			Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        	
	        Connection con = DriverManager.getConnection("jdbc:odbc:leftDB", "",""); 
			
   			Statement stmt = con.createStatement();
   			
   			if(comboLanguage.getSelectedIndex() == 1){
   				
   				query = "SELECT * FROM language1Dict ORDER BY word ASC";
   			}
   			else{
   				query = "SELECT * FROM language2Dict ORDER BY word ASC";
   			}
   			
   			ResultSet rs = stmt.executeQuery(query);
   			
   			String temp;
   			Vector v = new Vector();
   			
   			while(rs.next()){
   				
   				temp = rs.getString("word")	+ " --- " + rs.getString("pos");
   				v.addElement(temp);   				
   			}
   			
   			list.setListData(v);
   			
   			rs.close();
   			
   			stmt.close();
   			con.close();
   			repaint();
   			
   		}catch(Exception e){
   			
   			javax.swing.JOptionPane.showMessageDialog(this, "Error loading dictionary.", "View Dictionary", javax.swing.JOptionPane.OK_OPTION);
   			
   		}			
	}
	
}